CC=gcc
CXX=g++
CFLAGS=-Wall $(DEBUG) $(DEFINITIONS) $(CINCLUDES) $(CMISCFLAGS)
CPPFLAGS=-Wall -ansi $(DEBUG) $(EXCEPTIONS) $(CPPINCLUDES) $(DEFINITIONS) $(CPPMISCFLAGS)
DEBUG=-O3 -g3
CMISCFLAGS=
CPPMISCFLAGS=-DGIMI_CLIENT_API=10303
DEFINITIONS=-DLINUX_OS -DGIMI_NO_DATATYPE_INCLUDES
EXCEPTIONS=-fexceptions -ffast-math 
CINCLUDES=-I. -I../../scanmatch -I../../mcl -I$(GDIR)utils
CPPINCLUDES=-I. -I../../scanmatch -I../../mcl $(OCCU_INCLUDE) -I$(GDIR)glutImageGUI -I$(GDIR)utils \
						-I$(GIMDIR)/JPEGEncoder

LD=g++
LDFLAGS=
LIBS=-lrt -lpthread -lGL -lGLU -lglut -ljpeg

GDIR=../../../../
GIMDIR=../../../..
SRCSC=
#SRCSCPP=j2b2.cpp $(OCCU_SRC_CPP)
VPATH=
CPPGIMI=
CGIMI=

CSRCUTIL = $(GIMDIR)/utils/ownutils.c $(GIMDIR)/utils/owndebug.c
CPPIMAGEGUI = $(GIMDIR)/glutImageGUI/glutImageGUI.cpp  $(GIMDIR)/JPEGEncoder/JPEGEncoder.cpp

SRC_LOG=convert_log.cpp $(OCCU_SRC_CPP)  $(CPPMCL)
CSRC_LOG=$(CSRCUTIL)

SRC_OCCU_SLAM=occu_slam.cpp $(CPPSM) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_OCCU_SLAM=$(CSRCUTIL)

SRC_LASER_ODO=laser_odo.cpp $(CPPSM) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_LASER_ODO=$(CSRCUTIL)

SRC_OCCU_MCL=occu_mcl.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_OCCU_MCL=$(CSRCUTIL)

SRC_OCCU_SLAMCL=occu_slamcl.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_OCCU_SLAMCL=$(CSRCUTIL)

SRC_TOPO=topo.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_TOPO=$(CSRCUTIL)

SRC_TOPO_LOG=topo_log.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_TOPO_LOG=$(CSRCUTIL)

SRC_TOPOTOISTO_LOG=topo_toisto_log.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_TOPOTOISTO_LOG=$(CSRCUTIL)


SRC_TOPOSCAN_LOG=toposcan_log.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_TOPOSCAN_LOG=$(CSRCUTIL)

SRC_SCAN_LOG=scan_log.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_SCAN_LOG=$(CSRCUTIL)

SRC_MAPMATCHER=map_matcher.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_MAPMATCHER=$(CSRCUTIL)

SRC_MAPMATCH_BATCH=map_match_batch.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_MAPMATCH_BATCH=$(CSRCUTIL)

SRC_MAP_PROCESSOR=map_processor.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_MAP_PROCESSOR=$(CSRCUTIL)


SRC_MAPMATCHER_LOG=map_matcher_log.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_MAPMATCHER_LOG=$(CSRCUTIL)

SRC_MAPPLOT=map_plot.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_MAPPLOT=$(CSRCUTIL)

SRC_PLOT=plot_autmap.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_PLOT=$(CSRCUTIL)

SRC_ANIMATE=animate_results.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
CSRC_ANIMATE=$(CSRCUTIL)


#SRC_OCCU_J2B2_TEST=occu_j2b2_test.cpp $(CPPSM) $(CPPMCL) $(CPPIMAGEGUI) $(OCCU_SRC_CPP)
#CSRC_OCCU_J2B2_TEST=$(CSRCUTIL)

## Build GIMI and it's dependencies.
## Files are set to CGIMI and CPPGIMI variables
include Makefile.mcl
LNMDIR=$(GDIR)/LnMlib/src

include $(LNMDIR)/map/Makefile.occu 

CGIMI += $(CMCL)
CPPGIMI += $(CPPMCL) $(CPPSM)

## Construct final lists of CPP and C objects
#OBJSC=$(SRCSC:.c=.o) $(CGIMI:.c=.o)
#OBJSCPP=$(SRCSCPP:.cpp=.opp) $(CPPGIMI:.cpp=.opp)

OBJ_LOG_CPP=$(SRC_LOG:.cpp=.opp)
OBJ_LOG_C=$(CSRC_LOG:.c=.o)

OBJ_OCCU_CPP=$(SRC_OCCU_SLAM:.cpp=.opp)
OBJ_OCCU_C=$(CSRC_OCCU_SLAM:.c=.o)

OBJ_LASER_ODO=$(SRC_LASER_ODO:.cpp=.opp)
OBJ_LASER_ODO_C=$(CSRC_LASER_ODO:.c=.o)

OBJ_OCCU_MCL_CPP=$(SRC_OCCU_MCL:.cpp=.opp)
OBJ_OCCU_MCL_C=$(CSRC_OCCU_MCL:.c=.o)

#OBJ_OCCU_J2B2_TEST_CPP=$(SRC_OCCU_J2B2_TEST:.cpp=.opp)
#OBJ_OCCU_J2B2_TEST_C=$(CSRC_OCCU_J2B2_TEST:.c=.o)

OBJ_OCCU_SLAMCL_CPP=$(SRC_OCCU_SLAMCL:.cpp=.opp)
OBJ_OCCU_SLAMCL_C=$(CSRC_OCCU_SLAMCL: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o)


## Define compiles and links
COMPILEC=$(CC) $(CFLAGS) $(CINCLUDES) -c
COMPILECPP=$(CXX) $(CPPFLAGS) $(CPPINCLUDES) -c
LINK=$(LD) $(LDFLAGS) $(LIBS)

.PHONY: all clean

all: topo occu_slam occu_mcl occu_slamcl laser_odo logconvert map_matcher map_matcher_log map_plot map_processor topo_log toposcan_log scan_log map_match_batch plot_autmap topo_toisto_log animate_results

#j2b2: $(OBJSC) $(OBJSCPP)
#			$(LINK) -o $@ $(OBJSC) $(OBJSCPP)
		
logconvert: $(OBJ_LOG_CPP) $(OBJ_LOG_C)
						$(LINK) -o $@ $(OBJ_LOG_C) $(OBJ_LOG_CPP)
			
occu_slam: $(OBJ_OCCU_C) $(OBJ_OCCU_CPP)
						$(LINK) -o $@ $(OBJ_OCCU_C) $(OBJ_OCCU_CPP)
						
occu_mcl: $(OBJ_OCCU_MCL_C) $(OBJ_OCCU_MCL_CPP)
						$(LINK) -o $@ $(OBJ_OCCU_MCL_C) $(OBJ_OCCU_MCL_CPP)

occu_slamcl: $(OBJ_OCCU_SLAMCL_C) $(OBJ_OCCU_SLAMCL_CPP)
						$(LINK) -o $@ $(OBJ_OCCU_SLAMCL_C) $(OBJ_OCCU_SLAMCL_CPP)

#j2b2_test: $(OBJ_OCCU_J2B2_TEST_C) $(OBJ_OCCU_J2B2_TEST_CPP)
#						$(LINK) -o $@ $(OBJ_OCCU_J2B2_TEST_C) $(OBJ_OCCU_J2B2_TEST_CPP)
						
topo: $(OBJ_TOPO_C) $(OBJ_TOPO_CPP)
						$(LINK) -o $@ $(OBJ_TOPO_C) $(OBJ_TOPO_CPP)
						
topo_log: $(OBJ_TOPO_LOG_C) $(OBJ_TOPO_LOG_CPP)
						$(LINK) -o $@ $(OBJ_TOPO_LOG_C) $(OBJ_TOPO_LOG_CPP)

topo_toisto_log: $(OBJ_TOPOTOISTO_LOG_C) $(OBJ_TOPOTOISTO_LOG_CPP)
						$(LINK) -o $@ $(OBJ_TOPOTOISTO_LOG_C) $(OBJ_TOPOTOISTO_LOG_CPP)

toposcan_log: $(OBJ_TOPOSCAN_LOG_C) $(OBJ_TOPOSCAN_LOG_CPP)
						$(LINK) -o $@ $(OBJ_TOPOSCAN_LOG_C) $(OBJ_TOPOSCAN_LOG_CPP)

scan_log: $(OBJ_SCAN_LOG_C) $(OBJ_SCAN_LOG_CPP)
						$(LINK) -o $@ $(OBJ_SCAN_LOG_C) $(OBJ_SCAN_LOG_CPP)

map_matcher: $(OBJ_MAPMATCH_C) $(OBJ_MAPMATCH_CPP)
						$(LINK) -o $@ $(OBJ_MAPMATCH_C) $(OBJ_MAPMATCH_CPP)
						
map_match_batch: $(OBJ_MAPMATCH_BATCH_C) $(OBJ_MAPMATCH_BATCH_CPP)
						$(LINK) -o $@ $(OBJ_MAPMATCH_BATCH_C) $(OBJ_MAPMATCH_BATCH_CPP)

map_processor: $(OBJ_MAP_PROCESSOR_C) $(OBJ_MAP_PROCESSOR_CPP)
						$(LINK) -o $@ $(OBJ_MAP_PROCESSOR_C) $(OBJ_MAP_PROCESSOR_CPP)

map_matcher_log: $(OBJ_MAPMATCH_LOG_C) $(OBJ_MAPMATCH_LOG_CPP)
						$(LINK) -o $@ $(OBJ_MAPMATCH_LOG_C) $(OBJ_MAPMATCH_LOG_CPP)
						
map_plot: $(OBJ_MAPPLOT_LOG_C) $(OBJ_MAPPLOT_CPP)
						$(LINK) -o $@ $(OBJ_MAPPLOT_LOG_C) $(OBJ_MAPPLOT_CPP)
						
plot_autmap: $(OBJ_PLOT_C) $(OBJ_PLOT_CPP)
						$(LINK) -o $@ $(OBJ_PLOT_C) $(OBJ_PLOT_CPP)

animate_results: $(OBJ_ANIMATE_C) $(OBJ_ANIMATE_CPP)
						$(LINK) -o $@ $(OBJ_ANIMATE_C) $(OBJ_ANIMATE_CPP)

laser_odo: $(OBJ_LASER_ODO) $(OBJ_LASER_ODO_C)
						$(LINK) -o $@ $(OBJ_LASER_ODO_C) $(OBJ_LASER_ODO)
						
						
clean:
	rm -f $(OBJSC) $(OBJSCPP) $(OBJ_OCCU_MCL_C) $(OBJ_OCCU_MCL_CPP) $(OBJ_OCCU_CPP) \
		 $(OBJ_OCCU_C) $(OBJ_OCCU_SLAMCL_CPP) $(OBJ_OCCU_SLAMCL_C) *.opp *.o \
		topo map_plot occu_slam occu_mcl map_matcher map_matcher_log map_plot map_processor \
 		*.out *~ *.d *.dpp

## Compile rule for all .o (.c) file
%.o:    %.c
				$(COMPILEC) -o $@ $<

## Compile rule for all .opp (.cpp) files
%.opp:	%.cpp
				$(COMPILECPP) -o $@ $<

## Dependency generation rule: For all required '.d' files
#%.d:	  %.c
#				$(CC) -MM $(CFLAGS) $< > $@.$$$$; \
#				sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
#				rm -f $@.$$$$
#
## Dependency generation rule: For all required '.dpp' files
#%.dpp:	%.cpp
#				$(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
#				sed 's,\($*\)\.o[ :]*,\1.opp $@ : ,g' < $@.$$$$ > $@; \
#				rm -f $@.$$$$
#
#%.h:	%.h
#
# Include dependency rules
#-include $(SRCSCPP:.cpp=.dpp) $(CPPGIMI:.cpp=.dpp)
#-include $(SRCSC:.c=.d) $(CGIMI:.c=.d)
